package com.sooncode.project.focusservice.controller;

import com.sooncode.project.core.finder.Page;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.focusservice.model.DeviceLogin;
import com.sooncode.project.focusservice.model.Organization;
import com.sooncode.project.focusservice.model.OrganizationType;
import com.sooncode.project.focusservice.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("organization")
public class OrganizationController {
    @Autowired
    private OrganizationService service;

    @PostMapping("organization")
    public boolean add(@RequestParam Map map){
        map.put("apps",map.get("apps").toString().split(","));
        return service.add(map);
    }
    @PutMapping("organization")
    public boolean modify(@RequestParam String id,@RequestParam Map map){
        map.put("apps",map.get("apps").toString().split(","));
        return service.modify(id,map);
    }
    @DeleteMapping("organization")
    public boolean delete(@RequestParam String[] ids){
        for(String id:ids){
            service.delete(id);
        }
        return true;
    }
    @GetMapping("organizations")
    public Page<Organization> getPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return service.getPageList(pageSize,pageIndex-1,map);
    }
    @GetMapping("orgList")
    public List<Organization> getOrgList(){
        return service.getOrgList();
    }
    @GetMapping("organization")
    public Organization getById(@RequestParam String id) {
        return service.findById(id);
    }
    @GetMapping("byAccount")
    public Organization getByAccount(@RequestParam String accountId){
        return service.getByAccountId(accountId);
    }

    //机构类型
    @PostMapping("type")
    public boolean addType(@RequestParam Map map){
        return service.addType(map);
    }
    @PutMapping("type")
    public boolean modifyType(@RequestParam String id,@RequestParam Map map){
        return service.modifyType(id,map);
    }
    @DeleteMapping("type")
    public boolean deleteType(@RequestParam String[] ids){
        for(String id:ids){
            service.deleteType(id);
        }
        return true;
    }

    @GetMapping("types")
    public List<OrganizationType> getTypes(){
        return service.getTypeList();
    }

    @GetMapping("deviceLogins")
    public Page<DeviceLogin> getDeviceLogins(@RequestParam String id,@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        map.remove("id");
        Utils.mapValueTrim(map);
        return service.getLoginList(id,pageSize,pageIndex-1,map);
    }

    @GetMapping("macList")
    public List<Map> getMacList(@RequestParam String id){
        return service.getDeviceMacs(id);
    }
}
